<?
include_once ("classes/config.php");
require_once("classes/atomlib.php");


// Allocate all necessary memory for the image.
ini_set('memory_limit', '-1');


// Default feed is GunzBlazing promo content, change this as needed to add affiliate codes etc.


$feeds = array("http://www.cockcruisers.com/atom/packs/index.xml?hasflv=1");
//$feeds = array("http://www.nubiles.net/webmasters/promo/dumpatom.php");

$thumbs_folder = 'uploads/thumbs/';
$save_to_file = 1;
$image_quality = 100;
$image_type = 2;

foreach ($feeds as $feedurl) {
	
	echo "##########################<BR>\n";
	echo "Feed: ".$feedurl."<BR>\n";
	
	$parser = new AtomParser();
	$parser->debug = false;
	$parser->FILE = $feedurl;
	if(!$parser->parse()) {
		throw new Exception($parser->error);
	} else {
		$contents = Array();
		$contents = $parser->feed;
	
		$date_uploaded = date('d-m-y');
		$allow_comments = 'yes';
		$allow_embedding = 'yes';
		$rating_number_votes = 0;
		$rating_total_points = 0;
		$updated_rating = 0; 
		$public_private = 'public';
		$featured = 'yes';
		$approved = 'yes';
		$number_of_views = 0;
		$user_id = 18;
		$approved = 'yes';
	
		foreach ($contents->entries as $entry) {
	
			$thumb_url = '';
			$video_url = '';
			$video_length = '';
			$location_recorded = '';
			$sponsor_url = '';
			$sponsor_title = '';
			$buy_url = '';
			$playlist_url = '';
	
			$title = $entry->title[1];
			$description = $entry->content[1];
			$tags = '';
			$channel = '';
			$video_title = '';
	
			foreach ($entry->links as $link) {
				// 2257
				if ($link['title'] == "2257") {
					$location_recorded = $link['href'];

				// Paysite Link
				} else if ($link['type'] == "text/html" && $link['rel']=="alternate") {
					$sponsor_url = $link['href'];
					$sponsor_title = $link['title'];

				// Buy DVD
				} else if (preg_match('/Buy DVD/i',$link['title']) && $buy_url=='' && $link['type'] == "text/html" && $link['rel']=="related") {
					$buy_url = $link['href'];

				// XSPF Playlist
				} else if ($link['title']=='playlist' && $link['type'] == "text/xml" && $link['rel']=="related") {
					$playlist_url = $link['href'];


				}
			}
			
			$bFoundVid = false;
			foreach ($entry->links as $link) {
				$video_length = '';
				$video_url = '';
				$video_title = '';
				$thumb_url = '';

				if ($link['type'] == "video/x-flv" && $link['rel']=="enclosure") {

					$video_id = md5($link['href']);

					$video_length = $link['length'];
					$video_length = sec2time($video_length);
					if ($video_length["minutes"]>0) {
						$video_length = $video_length["minutes"]."m ".$video_length["seconds"]."s";
					} else {
						$video_length = $video_length["seconds"]."s";
					}
					$video_url = $link['href'];
					$video_title = $link['title'];
					
					foreach ($entry->links as $linka) {
						if ("image[".$video_title."]" == $linka['title'] && $linka['type'] == "image/jpeg" && $linka['rel']=="related") {
							$thumb_url = $linka['href'];
						}
					}
					
					if ($video_url!='') {
						echo "<BR><BR>\n\n";
						echo "approved: " . $approved . "<BR>\n";
						echo "video_id: " . $video_id . "<BR>\n";
						echo "title: " . $title . " (" . $video_title . ")<BR>\n";
						echo "description: " . $description . "<BR>\n";
						echo "tags: " . $tags . "<BR>\n";
						echo "channel: " . $channel . "<BR>\n";
						echo "date_uploaded: " . $date_uploaded . "<BR>\n";
						echo "location_recorded: " . $location_recorded . "<BR>\n";
						echo "video_length: " . $video_length . "<BR>\n";
						echo "allow_comments: " . $allow_comments . "<BR>\n";
						echo "allow_embedding: " . $allow_embedding . "<BR>\n";
						echo "rating_number_votes: " . $rating_number_votes . "<BR>\n";
						echo "rating_total_points: " . $rating_total_points . "<BR>\n";
						echo "updated_rating: " . $updated_rating . "<BR>\n";
						echo "public_private: " . $public_private . "<BR>\n";
						echo "featured: " . $featured . "<BR>\n";
						echo "approved: " . $approved . "<BR>\n";
						echo "number_of_views: " . $number_of_views . "<BR>\n";
						echo "user_id: " . $user_id . "<BR>\n";
						echo "sponsor_url: " . $sponsor_url . "<BR>\n";
						echo "video_url: " . $video_url . "<BR>\n";
						echo "thumb_url: " . $thumb_url . "<BR>\n";
						echo "buy_url: " . $buy_url . "<BR>\n";
			
						$sql = "INSERT INTO videos (video_id, title, description, tags, channel, date_uploaded, location_recorded, video_length, allow_comments, allow_embedding, rating_number_votes,rating_total_points, updated_rating, public_private, featured, approved, number_of_views, user_id, sponsor_title, sponsor_url, video_url, thumb_url, buy_url, playlist_url) VALUES ('$video_id', '".mysql_real_escape_string($title . ' (' . $video_title . ')')."', '".mysql_real_escape_string($description)."', '".mysql_real_escape_string($tags)."', '$channel', '$date_uploaded', '$location_recorded', '$video_length', '$allow_comments', '$allow_embedding', 0, 0, 0, '$public_private', 'yes', 'yes', 0, $user_id, '".mysql_real_escape_string($sponsor_title)."', '$sponsor_url', '$video_url', '$thumb_url', '$buy_url', '$playlist_url')";
						$res = mysql_query($sql);
//						echo "SQL: " . $sql . "<BR>\n";
						echo "res: " . $res . "<BR>\n";
			
						if ($thumb_url != "" && $res) {
							echo "Gen Thumb<BR>\n";
							GenerateThumbFile($thumb_url, $thumbs_folder . $video_id.'.jpg', 100, 75);
							$bFoundVid = true;
						}
					}
				}
			}
			
			// Use Playlist if no Videos found but Playlist is			
			if (!($bFoundVid) && $playlist_url!='') {				
				
				$video_url = $playlist_url;
				
				echo "<BR><BR>\n\n";
				echo "<b>Playlist Used</b><BR>\n";
				echo "approved: " . $approved . "<BR>\n";
				echo "video_id: " . $video_id . "<BR>\n";
				echo "title: " . $title . " (" . $video_title . ")<BR>\n";
				echo "description: " . $description . "<BR>\n";
				echo "tags: " . $tags . "<BR>\n";
				echo "channel: " . $channel . "<BR>\n";
				echo "date_uploaded: " . $date_uploaded . "<BR>\n";
				echo "location_recorded: " . $location_recorded . "<BR>\n";
				echo "video_length: " . $video_length . "<BR>\n";
				echo "allow_comments: " . $allow_comments . "<BR>\n";
				echo "allow_embedding: " . $allow_embedding . "<BR>\n";
				echo "rating_number_votes: " . $rating_number_votes . "<BR>\n";
				echo "rating_total_points: " . $rating_total_points . "<BR>\n";
				echo "updated_rating: " . $updated_rating . "<BR>\n";
				echo "public_private: " . $public_private . "<BR>\n";
				echo "featured: " . $featured . "<BR>\n";
				echo "approved: " . $approved . "<BR>\n";
				echo "number_of_views: " . $number_of_views . "<BR>\n";
				echo "user_id: " . $user_id . "<BR>\n";
				echo "sponsor_url: " . $sponsor_url . "<BR>\n";
				echo "video_url: " . $video_url . "<BR>\n";
				echo "thumb_url: " . $thumb_url . "<BR>\n";
				echo "buy_url: " . $buy_url . "<BR>\n";
	
				$sql = "INSERT INTO videos (video_id, title, description, tags, channel, date_uploaded, location_recorded, video_length, allow_comments, allow_embedding, rating_number_votes,rating_total_points, updated_rating, public_private, featured, approved, number_of_views, user_id, sponsor_title, sponsor_url, video_url, thumb_url, buy_url) VALUES ('$video_id', '".mysql_real_escape_string($title . ' (' . $video_title . ')')."', '".mysql_real_escape_string($description)."', '".mysql_real_escape_string($tags)."', '$channel', '$date_uploaded', '$location_recorded', '$video_length', '$allow_comments', '$allow_embedding', 0, 0, 0, '$public_private', 'yes', 'yes', 0, $user_id, '".mysql_real_escape_string($sponsor_title)."', '$sponsor_url', '$video_url', '$thumb_url', '$buy_url')";
				$res = mysql_query($sql);
//						echo "SQL: " . $sql . "<BR>\n";
				echo "res: " . $res . "<BR>\n";
	
				if ($thumb_url != "" && $res) {
					echo "Gen Thumb<BR>\n";
					GenerateThumbFile($thumb_url, $thumbs_folder . $video_id.'.jpg', 100, 75);
				}
				
			}
		}
	}
}



function sec2time($time){
	if(is_numeric($time)){
		$value = array(
			"years" => 0, "days" => 0, "hours" => 0,
			"minutes" => 0, "seconds" => 0,
		);
		if($time >= 31556926) {
			$value["years"] = floor($time/31556926);
			$time = ($time%31556926);
		}
		if($time >= 86400) {
			$value["days"] = floor($time/86400);
			$time = ($time%86400);
		}
		if($time >= 3600) {
			$value["hours"] = floor($time/3600);
			$time = ($time%3600);
		}
		if($time >= 60) {
			$value["minutes"] = floor($time/60);
			$time = ($time%60);
		}
		$value["seconds"] = floor($time);
		return (array) $value;
	} else {
		return (bool) FALSE;
	}
}

function SaveImage($type, $im, $filename, $quality, $to_file = true) {
	$res = null;
	if (!function_exists('imagegif')) $type = 3;
	switch ($type) {
		case 1:
			if ($to_file) {
				$res = ImageGIF($im, $filename);
			} else {
				header("Content-type: image/gif");
				$res = ImageGIF($im);
			}
			break;
		case 2:
			if ($to_file) {
				$res = ImageJPEG($im, $filename, $quality);
			} else {
				header("Content-type: image/jpeg");
				$res = ImageJPEG($im, '', $quality);
			}
			break;
		case 3:
			if ($to_file) {
				$res = ImagePNG($im, $filename);
			} else {
				header("Content-type: image/png");
				$res = ImagePNG($im, '', $quality);
			}
			break;
	}
	imagedestroy($im);
	return $res;
}

function ImageCreateFromType($type, $filename) {
	$im = null;
	switch ($type) {
		case 1:
			$im = ImageCreateFromGif($filename);
			break;
		case 2:
			$im = ImageCreateFromJpeg($filename);
			break;
		case 3:
			$im = ImageCreateFromPNG($filename);
			break;
	}
	return $im;
}

function GenerateThumbFile($from_name, $to_name, $max_x, $max_y) {
	global $save_to_file, $image_type, $image_quality;
	// get source image size (width/height/type)
	// orig_img_type 1 = GIF, 2 = JPG, 3 = PNG
	list($orig_x, $orig_y, $orig_img_type, $img_sizes) = GetImageSize($from_name);
	// should we override thumb image type?
	$image_type = ($image_type != -1 ? $image_type : $orig_img_type);
	// check for allowed image types
	if ($orig_img_type < 1 or $orig_img_type > 3) die("Image type not supported");
	if ($orig_x > $max_x or $orig_y > $max_y) {
		// resize
		$per_x = $orig_x / $max_x;
		$per_y = $orig_y / $max_y;
		if ($per_y > $per_x) {
			$max_x = $orig_x / $per_y;
		} else {
			$max_y = $orig_y / $per_x;
		}
	} else {
		// keep original sizes, i.e. just copy
		if ($save_to_file) {
			@copy($from_name, $to_name);
		} else {
			switch ($image_type) {
				case 1:
					header("Content-type: image/gif");
					readfile($from_name);
					break;
				case 2:
					header("Content-type: image/jpeg");
					readfile($from_name);
					break;
				case 3:
					header("Content-type: image/png");
					readfile($from_name);
					break;
			}
		}
		return;
	}
	if ($image_type == 1) {
		// should use this function for gifs (gifs are palette images)
		$ni = imagecreate($max_x, $max_y);
	} else {
		// Create a new true color image
		$ni = ImageCreateTrueColor($max_x, $max_y);
	}
	// Fill image with white background (255,255,255)
	$white = imagecolorallocate($ni, 255, 255, 255);
	imagefilledrectangle($ni, 0, 0, $max_x, $max_y, $white);
	// //////////////////////
	// START RUNNING SCRIPT
	// //////////////////////
	// Create a new image from source file
	$im = ImageCreateFromType($orig_img_type, $from_name);
	// Copy the palette from one image to another
	imagepalettecopy($ni, $im);
	// Copy and resize part of an image with resampling
	imagecopyresampled(
		$ni, $im, // destination, source
		0, 0, 0, 0, // dstX, dstY, srcX, srcY
		$max_x, $max_y, // dstW, dstH
		$orig_x, $orig_y); // srcW, srcH
	// save thumb file
	SaveImage($image_type, $ni, $to_name, $image_quality, $save_to_file) . "<BR>\n";
	if ($temp) {
		unlink($tmpfname); // this removes the file
	}
}

?>